package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.admin.OrdersMapper;
import com.sky.mapper.user.OrderDetailMapper;
import com.sky.result.PageResult;
import com.sky.service.admin.OrdersService;
import com.sky.vo.OrderSearchVo;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service("adminOrdersService")
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 订单搜索接口
     * @param ordersPageQueryDTO
     * @return
     */
    @Override
    public PageResult condition(OrdersPageQueryDTO ordersPageQueryDTO) {
        // 查询订单概览
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        Page<OrderSearchVo> orderSearchVoPage = ordersMapper.selectPage(ordersPageQueryDTO);
        List<OrderSearchVo> result = orderSearchVoPage.getResult();

        // 查询订单详情数据并插入
        for (OrderSearchVo orderSearchVo : result) {
            List<OrderDetail> orderDetails = orderDetailMapper.selectById(orderSearchVo.getId());
            orderSearchVo.setOrderDetailList(orderDetails);
            // 拼接菜品名称
            String orderDishes = orderDetails.stream()
                    .map(orderDetail -> "'" + orderDetail.getName() + "'")
                    .collect(Collectors.joining(" "));
//            log.info("打印的菜单内容{}",orderDishes);
            orderSearchVo.setOrderDishes(orderDishes);
        }

        return new PageResult(orderSearchVoPage.getTotal(), orderSearchVoPage.getResult());

    }

    /**
     * 各个状态订单的数量统计
     * @return
     */
    @Override
    public OrderStatisticsVO statistics() {
        // 查询各个状态的订单数量并返回
        OrderStatisticsVO orderStatisticsVO = OrderStatisticsVO.builder()
                .toBeConfirmed(ordersMapper.selectStatus(Orders.TO_BE_CONFIRMED))
                .confirmed(ordersMapper.selectStatus(Orders.CONFIRMED))
                .deliveryInProgress(ordersMapper.selectStatus(Orders.DELIVERY_IN_PROGRESS))
                .build();
        log.info("不同类型的订单{}", orderStatisticsVO);

        return orderStatisticsVO;
    }

    /**
     * 接单
     * @param ordersConfirmDTO
     */
    @Override
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = Orders.builder()
                .id(ordersConfirmDTO.getId())
                .status(Orders.CONFIRMED)
                .build();

        ordersMapper.update(orders);
    }

    /**
     * 拒单
     * @param ordersRejectionDTO
     */
    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders = Orders.builder()
                .id(ordersRejectionDTO.getId())
                .status(Orders.CANCELLED)
                .rejectionReason(ordersRejectionDTO.getRejectionReason())
                .payStatus(Orders.REFUND)
                .build();

        ordersMapper.update(orders);
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     */
    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        Orders orders = Orders.builder()
                .id(ordersCancelDTO.getId())
                .status(Orders.CANCELLED)
                .cancelTime(LocalDateTime.now())
                .build();
        ordersMapper.update(orders);
    }

    /**
     * 派送订单
     * @param id
     */
    @Override
    public void delivery(Long id) {
        Orders orders = Orders.builder()
                .id(id)
                .status(Orders.DELIVERY_IN_PROGRESS)
                .build();
        ordersMapper.update(orders);
    }

    /**
     * 完成订单
     * @param id
     */
    @Override
    public void complete(Long id) {
        Orders orders = Orders.builder()
                .id(id)
                .status(Orders.COMPLETED)
                .build();
        ordersMapper.update(orders);

    }

    /**
     * 查询订单详情
     * @param id
     * @return orderVo
     */
    @Override
    public OrderVO selectById(Long id) {

        // 先查询订单表
        Orders orders =  ordersMapper.selectById(id);
        // 再查询订单详情表
        List<OrderDetail> orderDetails = orderDetailMapper.selectById(id);
        // 插入并封装为VO对象
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderDetailList(orderDetails);
        BeanUtils.copyProperties(orders, orderVO);

        return orderVO;
    }


}
